package cn.iocoder.yudao.module.crm.service.task;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.task.vo.TaskPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.task.vo.TaskSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.task.TaskDO;
import cn.iocoder.yudao.module.crm.dal.mysql.task.TaskMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeMapper;
import cn.iocoder.yudao.module.crm.enums.task.CrmTaskOperationStatus;
import cn.iocoder.yudao.module.crm.service.tasktype.TaskTypeAssigneeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.Random;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.TASK_NOT_EXISTS;

/**
 * CRM 任务单 Service 实现类
 *
 * @author 广州氪量
 */
@Service
@Validated
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private TaskTypeMapper taskTypeMapper;
    @Resource
    private TaskOperationLogService taskOperationLogService;
    @Resource
    private TaskApproverService taskApproverService;
    @Resource
    private TaskContentService taskContentService;
    @Resource
    private TaskTypeAssigneeService taskTypeAssigneeService;

    @Override
    public Long createTask(TaskSaveReqVO createReqVO) {
        // 编码自动生成，下发时间以当下时间为准
        createReqVO.setCode(generateCode());
        createReqVO.setIssueTime(LocalDateTime.now());
        // 插入
        TaskDO task = BeanUtils.toBean(createReqVO, TaskDO.class);
        taskMapper.insert(task);
        // 根据任务单类型的设置，添加任务单相关联系
        taskApproverService.createTaskApprover(task.getTaskTypeId(),task.getId());
        taskContentService.createTaskContent(task.getTaskTypeId(),task.getId());
        taskOperationLogService.createTaskOperationLog(task.getId(), 0, 0L);
        // 返回
        return task.getId();
    }

    @Override
    public Long updateTask(TaskSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskExists(updateReqVO.getId());
        // 更新
        TaskDO task = BeanUtils.toBean(updateReqVO, TaskDO.class);
        taskMapper.updateById(task);
        // 根据任务单类型的设置，删除任务单相关联系后重新添加
        taskApproverService.deleteTaskApprover(task.getId());
        taskApproverService.createTaskApprover(task.getTaskTypeId(),task.getId());
        taskContentService.deleteTaskContent(task.getId());
        taskContentService.createTaskContent(task.getTaskTypeId(),task.getId());
        taskOperationLogService.createTaskOperationLog(task.getTaskTypeId(), 0, 0L);
        return task.getId();
    }

    @Override
    public void deleteTask(Long id) {
        // 校验存在
        validateTaskExists(id);
        // 删除
        taskMapper.deleteById(id);
    }

    private void validateTaskExists(Long id) {
        if (taskMapper.selectById(id) == null) {
            throw exception(TASK_NOT_EXISTS);
        }
    }

    @Override
    public TaskDO getTask(Long id) {
        return taskMapper.selectById(id);
    }

    @Override
    public PageResult<TaskDO> getTaskPage(TaskPageReqVO pageReqVO) {
        return taskMapper.selectPage(pageReqVO);
    }

    /**
     * 生成的编码字符串
     * @return 生成的编码字符串
     */
    public static String generateCode() {
        StringBuilder codeBuilder = new StringBuilder(9);
        String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random RANDOM = new Random();
        // 添加3个随机大写字母
        for (int i = 0; i < 3; i++) {
            int index = RANDOM.nextInt(CHARACTERS.length());
            codeBuilder.append(CHARACTERS.charAt(index));
        }
        // 添加6位随机数字
        for (int i = 0; i < 6; i++) {
            int digit = RANDOM.nextInt(10);
            codeBuilder.append(digit);
        }
        return codeBuilder.toString();
    }
}